﻿#include "FSlider.h"
#include "../Base/FTheme.h"

#include <QPainter>
#include <QDebug>

FSlider::FSlider(QWidget *parent)
    : QSlider(Qt::Horizontal, parent), m_lightPos(0.0)
{
    setFixedHeight(14);
    setCursor(Qt::PointingHandCursor);
    setStyleSheet("margin: 25px;");

    // 流光动画
    m_lightAnim = new QPropertyAnimation(this, "lightPos");
    m_lightAnim->setStartValue(0);
    m_lightAnim->setEndValue(1);
    m_lightAnim->setDuration(2000);
    m_lightAnim->setLoopCount(-1);
    m_lightAnim->start();
}

void FSlider::setLightPos(double pos)
{
    m_lightPos = pos;
    update();
}

void FSlider::setLightAnimEnabled(bool en)
{
    m_animEn = en;
}

void FSlider::paintEvent(QPaintEvent *ev)
{
    Q_UNUSED(ev);

    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);

    int h = height();
    QRect fullRect = rect().adjusted(6, h/3, -6, -h/3);
    int radius = fullRect.height() / 2;

    // 计算已选区域宽度
    double ratio = double(value()-minimum()) / (maximum()-minimum());
    QRectF progRect(fullRect.left(), fullRect.top(), fullRect.width()*ratio, fullRect.height());

    // 背景槽
    p.setBrush(FColors::getBaseColor(FColors::Default));
    p.setPen(Qt::NoPen);
    p.drawRoundedRect(fullRect, radius, radius);

    // 进度渐变
    if (ratio > 0) {
        QLinearGradient grad(fullRect.topLeft(), fullRect.topRight());
        const auto& linears = FColors::getLinearColors(FColors::Primary, 2);
        grad.setColorAt(0, linears[0]);
        grad.setColorAt(1, linears[1]);

        p.setBrush(grad);
        p.setPen(Qt::NoPen);

        p.drawRoundedRect(progRect, radius, radius);
    }

    // 流光(裁剪在进度区域内)
    if (ratio > 0 && m_animEn) {
        double lightWidth = width() * 0.18;
        double startX = progRect.width() * m_lightPos;

        QRectF lightRect(startX - lightWidth/2, fullRect.y(), lightWidth, fullRect.height());

        QLinearGradient glow(lightRect.topLeft(), lightRect.topRight());
        glow.setColorAt(0.0, QColor(255,255,255, 0));
        glow.setColorAt(0.5, QColor(255,255,255,140));
        glow.setColorAt(1.0, QColor(255,255,255, 0));

        p.save();
        p.setClipRect(progRect);

        p.setBrush(glow);
        p.setPen(Qt::NoPen);
        p.drawRoundedRect(lightRect, radius, radius);
        p.restore();
    }

    // ---- 绘制滑块 ----
    int handleX = fullRect.left() + progRect.width() - h/2;
    double scale = m_pressed ? 0.85 : (m_hover ? 1.1 : 1.0);

    QRectF handleRect(handleX, 2, h*0.9*scale, h*0.9*scale);
    handleRect.moveCenter(QPointF(handleX + h*0.45, h/2));

    // 颜色
    QColor base = FColors::getBaseColor(FColors::Primary);
    QColor hover = FColors::getHoverColor(FColors::Primary);
    QColor press = FColors::getActiveColor(FColors::Primary);

    QColor color;
    if (m_pressed)
        color = press;
    else if (m_hover)
        color = hover;
    else
        color = base;

    // 圆球
    p.setBrush(color);
    p.setPen(Qt::NoPen);
    p.drawEllipse(handleRect);
}

void FSlider::enterEvent(QEvent *e)
{
    m_hover = true;
    update();
    QSlider::enterEvent(e);
}

void FSlider::leaveEvent(QEvent *e)
{
    m_hover = false;
    update();
    QSlider::leaveEvent(e);
}

void FSlider::mousePressEvent(QMouseEvent *e)
{
    m_pressed = true;
    update();
    QSlider::mousePressEvent(e);
}

void FSlider::mouseReleaseEvent(QMouseEvent *e)
{
    m_pressed = false;
    update();
    QSlider::mouseReleaseEvent(e);
}
